package com.flyqiu.form.core.event;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.form.api.FlyQiuFormContext;
import com.flyqiu.form.api.col.FlyQiuStoreColTypeManage;
import com.flyqiu.form.api.event.FlyQiuTableEvent;
import com.flyqiu.form.api.tools.FlyQiuAvueColTools;
import com.flyqiu.form.api.tools.FlyQiuTableOptionTools;
import com.flyqiu.form.common.col.SqlColType;
import com.flyqiu.form.model.SqlColumnModel;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;

import java.util.List;
import java.util.Map;

import static com.flyqiu.form.common.FlyQiuFormConstant.FormBaseColumns.*;

public class GlobalFlyQiuTableEvent implements FlyQiuTableEvent {
    private final FlyQiuStoreColTypeManage storeColTypeManage;

    public GlobalFlyQiuTableEvent(FlyQiuFormContext context) {
        this.storeColTypeManage = context.getStoreColTypeManage();
    }


    @Override
    public void beforeCreateTable(FlyQiuTableConfig formConfig) {
        JSONArray columns = FlyQiuTableOptionTools.getColumns(formConfig);
        autoAppendDefaultField(formConfig, columns);
    }

    @Override
    public void beforeUpdateTable(FlyQiuTableConfig formConfig, List<SqlColumnModel> oldColumn) {
        JSONArray columns = FlyQiuTableOptionTools.getColumns(formConfig);
        autoAppendDefaultField(formConfig, columns);
    }

    @Override
    public void beforeSynchronizationTable(FlyQiuTableConfig formConfig) {
        JSONArray columns = FlyQiuTableOptionTools.getColumns(formConfig);
        autoAppendDefaultField(formConfig, columns);
    }


    /**
     * 主表追加默认字段
     *
     * @param formConfig 表单配置
     * @param column     列信息
     */
    private void autoAppendDefaultField(FlyQiuTableConfig formConfig, JSONArray column) {
        Map<String, Integer> columnsMap = FlyQiuTableOptionTools.getTableOptionFieldIndex(column);
        handlerAvueDynamicDefaultField(formConfig, column);
        SqlColType sqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_PK_NAME);
        FlyQiuAvueColTools.autoAppendField(columnsMap, column, COLUMN_PK_NAME, "主键", sqlColType, 0);
        sqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_PK_NAME);
        FlyQiuAvueColTools.autoAppendField(columnsMap, column, COLUMN_CREATE_TIME, "创建时间", sqlColType);
        sqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_DELETE_FLAG);
        FlyQiuAvueColTools.autoAppendField(columnsMap, column, COLUMN_DELETE_FLAG, "删除标记", sqlColType);

    }

    /**
     * 处理子表单字段追加
     *
     * @param formConfig 表单配置
     * @param column     列信息
     */
    private void handlerAvueDynamicDefaultField(FlyQiuTableConfig formConfig, JSONArray column) {
        SqlColType pkSqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_PK_NAME);
        SqlColType createTimeSqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_PK_NAME);
        SqlColType delFlagSqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_DELETE_FLAG);
        SqlColType mainTableSqlColType = storeColTypeManage.getStoreColType(formConfig.getDataStorageType(), COLUMN_MAIN_TABLE_ID);
        for (int i = 0; i < column.size(); i++) {
            JSONObject dynamic = column.getJSONObject(i);
            if (!FlyQiuAvueColTools.isDynamic(dynamic)) {
                continue;
            }
            JSONObject children = dynamic.getJSONObject("children");
            JSONArray dynamicColumns = children.getJSONArray("column");
            Map<String, Integer> dynamicColumnsMap = FlyQiuTableOptionTools.getTableOptionFieldIndex(dynamicColumns);

            FlyQiuAvueColTools.autoAppendField(dynamicColumnsMap, dynamicColumns, COLUMN_PK_NAME, "主键", pkSqlColType, 0);
            FlyQiuAvueColTools.autoAppendField(dynamicColumnsMap, dynamicColumns, COLUMN_MAIN_TABLE_ID, "主表id", mainTableSqlColType, 1);
            FlyQiuAvueColTools.autoAppendField(dynamicColumnsMap, dynamicColumns, COLUMN_CREATE_TIME, "创建时间", createTimeSqlColType);
            FlyQiuAvueColTools.autoAppendField(dynamicColumnsMap, dynamicColumns, COLUMN_DELETE_FLAG, "删除标记", delFlagSqlColType);
        }
    }

}
